﻿using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace GameService
{
    class Program
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        static void Main(string[] args)
        {

#if DEBUG
            logger.Info("==========开始使用控制台调试Windows服务==========");
            var WinService = new WindowsService();
            WinService.MyTimerTest();
            
            Console.ReadLine();
            Console.ReadKey();
#else
            ///带参启动运行服务
            if (args.Length > 0)
            {
                try
                {
                    ServiceBase[] serviceToRun = new ServiceBase[] { new WindowsService() };
                    ServiceBase.Run(serviceToRun);
                }
                catch (Exception ex)
                {
                    logger.Error("\nService Start Error：" + DateTime.Now.ToString() + "\n" + ex.Message);
                }
            }
            //不带参启动配置程序
            else
            {
                StartLable:
                logger.Info("\n\n请选择你要执行的操作——1：自动部署服务，2：安装服务，3：卸载服务，4：验证服务状态，5：退出");
                logger.Info("————————————————————");
                ConsoleKey key = Console.ReadKey().Key;
                
                if (key == ConsoleKey.NumPad1 || key == ConsoleKey.D1)
                {
                    if (ServiceHelper.IsServiceExisted("SSFHService"))
                    {
                        ServiceHelper.ConfigService("SSFHService", false);
                    }
                    if (!ServiceHelper.IsServiceExisted("SSFHService"))
                    {
                        ServiceHelper.ConfigService("SSFHService", true);
                    }
                    ServiceHelper.StartService("SSFHService");
                    goto StartLable;
                }
                else if (key == ConsoleKey.NumPad2 || key == ConsoleKey.D2)
                {
                    if (!ServiceHelper.IsServiceExisted("SSFHService"))
                    {
                        ServiceHelper.ConfigService("SSFHService", true);
                    }
                    else
                    {
                        logger.Info("\n服务已存在......");
                    }
                    goto StartLable;
                }
                else if (key == ConsoleKey.NumPad3 || key == ConsoleKey.D3)
                {
                    if (ServiceHelper.IsServiceExisted("SSFHService"))
                    {
                        ServiceHelper.ConfigService("SSFHService", false);
                    }
                    else
                    {
                        logger.Info("\n服务不存在......");
                    }
                    goto StartLable;
                }
                else if (key == ConsoleKey.NumPad4 || key == ConsoleKey.D4)
                {
                    if (!ServiceHelper.IsServiceExisted("SSFHService"))
                    {
                        logger.Info("\n服务不存在......");
                    }
                    else
                    {
                        logger.Info("\n服务状态：" + ServiceHelper.GetServiceStatus("SSFHService").ToString());
                    }
                    goto StartLable;
                }
                else if (key == ConsoleKey.NumPad5 || key == ConsoleKey.D5) { }
                else
                {
                    logger.Info("\n请输入一个有效键！");
                    logger.Info("————————————————————");
                    goto StartLable;
                }
            }
#endif
        }
    }
}
